<?php

namespace App\Http\Controllers\App;

use App\Models\Block;
use App\Models\BlockType;
use Illuminate\Http\Request;

class BlockController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if($request->ajax()){
            $model = Block::query();
            if ($request->get('block_type_id')){
                $model = $model->where('block_type_id',$request->get('block_type_id'));
            }
            if ($request->get('title')){
                $model = $model->where('title','like','%'.$request->get('title').'%');
            }
            $res = $model->orderBy('sort','desc')->orderBy('id','desc')->with('block_type')->paginate($request->get('limit',30))->toArray();
            $data = [
                'code' => 0,
                'msg'   => '正在请求中...',
                'count' => $res['total'],
                'data'  => $res['data']
            ];
            return response()->json($data);
        }

        $block_types = BlockType::Lists();
        return view('admin.app.content.block.index',compact('block_types'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $block_types = BlockType::Lists();
        return view('admin.app.content.block.create',compact('block_types'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'title'  => 'required|string',
            'sort'  => 'required|numeric',
            'thumb' => 'required|string',
            'position_id' => 'required|numeric'
        ]);
        if (Block::create($request->all())){
            return $this->success('添加完成');
        }
        return $this->error('系统错误');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $block = Block::findOrFail($id);
        $block_types = BlockType::Lists();
        foreach ($block_types as $block_type){
            $block_type->selected = $block_type->id == $block->block_type_id ? 'selected' : '';
        }
        return view('admin.app.content.block.edit',compact('block_types','block'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'title'  => 'required|string',
            'sort'  => 'required|numeric',
            'thumb' => 'required|string',
            'position_id' => 'required|numeric'
        ]);
        $block = Block::findOrFail($id);
        if ($block->update($request->all())){
            return $this->success('更新成功');
        }
        return $this->error('系统错误');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return $this->error('请选择删除项');
        }
        if (Block::destroy($ids)){
            return $this->success('删除成功');
        }
        return $this->error('删除失败');
    }
}
